﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Providers;
using MethodWorx.CMS.Workflow;

namespace MethodWorx.CMS.Plugins
{
    public abstract class Plugin : IPlugin, IIdePlugin
    {
        public ICmsDataProvider CmsDataProvider { get; private set; }
        private List<IdeExtension> extensions = new List<IdeExtension>();

        public virtual Guid Id
        {
            get { return Guid.Parse(this.GetAttribute().Id); }
        }

        public virtual string Name
        {
            get { return this.GetAttribute().Name; }
        }

        public virtual string Description
        {
            get { return this.GetAttribute().Description; }
        }

        public virtual  void SetProvider(Providers.ICmsDataProvider provider)
        {
            this.CmsDataProvider = provider;
        }

        private CmsPluginAttribute GetAttribute()
        {
            var attr = this.GetType().GetCustomAttributes(true).OfType<CmsPluginAttribute>().FirstOrDefault();
            if (attr == null)
                throw new Exception("Plugin class does is not decorated with the mandatory CmsPluginAttribute");

            if (Guid.Parse(attr.Id) == Guid.Empty)
                throw new Exception("Plugin class does is not decorated with the mandatory CmsPluginAttribute");

            return attr;
        }



        public void RegisterIdeExtension(IdeExtension extension)
        {
            this.extensions.Add(extension);
        }

        public IEnumerable<IdeExtension> GetIdeExtensionsForPageTemplate(Content.PageTemplate template)
        {
            return new IdeExtension[] {};
        }

        public IEnumerable<IdeExtension> GetIdeExtensionsForPage(Content.Page page)
        {
            return new IdeExtension[] {};
        }

        public IEnumerable<IdeExtension> GetIdeExtensions()
        {
            return this.extensions.AsReadOnly();
        }
    }
}
